<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- vue3 <script src="https://unpkg.com/vue@next"></script> -->
    vue2
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> -->
  </head>
  <body>
    <div id="app">
      <h2>color: {{color}}</h2>
    </div>

    <script>
      let data = {
        color: 'red',
        shape: 'square'
      }

      let tmp = 'red'
      // setInterval(() => {
      //   if (data.color !== tmp) {
      //     console.log('color 发生变化了')
      //     tmp = data.color
      //   }
      // }, 100)

      // 创建一个监测的实例对象, 用于监视data属性的变化
      function Observer(obj) {
        // 汇总对象中所有的属性形成一个数组
        const keys = Object.keys(obj)
        console.log(keys)
        keys.forEach(k => {
          Object.defineProperty(this, k, {
            set(val) {
              this.k = val
            },
            get() {
              return obj[k]
            }
          })
        })
      }

      let obs = new Observer(data)
      function Vue(options) {}
      const vm = new Vue()

      vm._data = data = obs
      // 怎样 让 vm 直接访问 vm.color ? 而不是 vm_data.color

      console.log(vm)
      /* const App = {
        data() {
          return data
        }
      }
      const app = Vue.createApp(App)
      console.log('app', app)
      app.mount('#app') */
    </script>
  </body>
</html>
